package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.enums.Coord;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 使用活力
 */
@Slf4j
public class UseEnergyTaskImpl extends AbstractTask {

    public UseEnergyTaskImpl(WinDef.HWND hwnd_0) {
        super(hwnd_0);
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc3();
        return true;
    }

    @Override
    public Boolean execute() throws Exception {
        // 点击 右上头像
        MouseUtil.lClickRandom(hwnd_0, Coord.ROLE_LOCATION.getX(), Coord.ROLE_LOCATION.getY());
        ThreadUtil.sleep1();

        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_使用活力, 5)) {
            log.error("【{}】没有找到 使用活力", getTaskName());
            return false;
        }
        ThreadUtil.sleep1();

        // 活力是否足够
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活力足够, 2).isFinded()) {
            log.info("【{}】活力不足", getTaskName());
            TaskProgressHandler.updateProgress(getTaskName(), "活力不足");
            clickEsc3();
            return false;
        }

        pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_制作临时符, 5);
        if (!pic.isFinded()) {
            log.error("【{}】没有找到 制作临时符", getTaskName());
            return false;
        }
        // 活力全部用完
        for (int i = 0; i < 10; i++) {
            MouseUtil.lClickRandom(hwnd_0, pic.getPicX() + Coord.MAKE_SYMBOL_LOCATION.getX(), pic.getPicY() + Coord.MAKE_SYMBOL_LOCATION.getY());
        }
        log.info("【{}】已全部使用活力制作符", getTaskName());

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        clickEsc3();
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "使用活力";
    }
}
